package ECommerce;

public class CustomerView {
    /*先创建一个数组*/
    CustomerList customerList=new CustomerList(10);
    //构造器，往数组里面放一个对象
    public CustomerView() {
        Customer customer = new Customer("james", '男', 37, "13222222222", "james@gmail.com");
        customerList.addCustomer(customer);
    }
    /*初始化页面*/
    public static void main(String[] args) {
        CustomerView view=new CustomerView();
        view.enterMenu();
    }

    public void enterMenu(){
        boolean isFlag=true;
        while (isFlag){
            System.out.println("\n--------------------管理系统--------------------\n");
            System.out.println("\t\t\t\t\t\t ① 添加客户");
            System.out.println("\t\t\t\t\t\t ② 修改客户");
            System.out.println("\t\t\t\t\t\t ③ 删除客户");
            System.out.println("\t\t\t\t\t\t ④ 客户列表");
            System.out.println("\t\t\t\t\t\t ⑤ 查找客户");
            System.out.println("\t\t\t\t\t\t ⑥ 退出\n");
            System.out.println("\t\t\t\t\t\t 请选择①-⑥");

            char key=CMUtility.readMenuSelection();
            switch (key){
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    selectCustomer();
                    break;
                case '6':
                    System.out.print("确认是否退出(Y/N)");
                    char isExit=CMUtility.readConfirmSelection();
                    if(isExit=='Y')
                    {
                        isFlag=false;
                    }
                    break;
            }


        }
    }
    /*添加用户操作*/
    private void addNewCustomer(){
        System.out.println("-----------添加客户----------");
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别：");
        char gender=CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(13);
        System.out.print("邮箱：");
        String email = CMUtility.readString(30);

        //将上述数据封装到对象中
        Customer customer=new Customer(name, gender, age, phone, email);
        //将该创建的对象的存入到customerList数组里面，通过返回值判断添加成功或失败
        boolean isSuccess=customerList.addCustomer(customer);
        if(isSuccess)
        {
            System.out.println("--------------添加完成----------");
        }else
        {
            System.out.println("------------客户目录已满，添加失败-------");
        }
    }
    /*修改客户数据，根据数组下标寻找*/
    private void modifyCustomer(){
        System.out.println("------------------修改客户------------");
        Customer cust; //声明在外边，在循环中修改，除了循环还可以使用
        int num;
        while(true)
        {
            System.out.print("请选择待修改客户的编号(-1退出)");
            num = CMUtility.readInt();
            if(num==-1)
            {
                return;
                //break;都可
            }

            cust=customerList.getCustomer(num-1);//此处num-1是因为用户输入1相当于操作数组中的第0个元素
            if(cust==null)
            {
                System.out.println("无法找到指定用户！");
            }else//找到了相应的用户
            {
                break;//跳出while循环
            }
        }
        //修改客户信息
        System.out.println("姓名("+cust.getName()+"):");
        String name = CMUtility.readString(10, cust.getName());//没有修改还是按照原来的
        System.out.println("性别("+cust.getGender()+"):");
        char gender = CMUtility.readChar(cust.getGender());
        System.out.println("年龄("+cust.getAge()+"):");
        int age = CMUtility.readInt(cust.getAge());
        System.out.println("电话("+cust.getPhone()+"):");
        String phone = CMUtility.readString(13, cust.getPhone());
        System.out.println("邮箱("+cust.getEmail()+"):");
        String email = CMUtility.readString(30, cust.getEmail());

        //将上述数据封装到这个新的对象中
        Customer newCust=new Customer(name, gender, age, phone, email);
        //替换这个数组下标位置的对象，通过返回值判断修改成功或失败
        boolean isReplaced= customerList.replaceCustomer(num-1, newCust);//此处的num-1是因为用户输入的num-1才是数组中要操作的位置的数

        if(isReplaced)
        {
            System.out.println("--------------修改完成-----------");
        }else
        {
            System.out.println("--------------修改失败-----------");
        }

    }
    private void deleteCustomer(){
        System.out.println("------------------------删除客户--------------");
        Customer cust;
        int num;
        while(true)
        {

            System.out.print("请选择待删除客户的编号(-1退出)");
            num = CMUtility.readInt();
            if(num==-1)
            {
                return;
            }
            cust = customerList.getCustomer(num-1);//
            if(cust==null)
            {
                System.out.println("无法找到指定客户！");
            }else
            {
                break;
            }


        }
        //找到了指定客户
        System.out.print("是否确认删除(Y/N)");
        char isDelete = CMUtility.readConfirmSelection();
        if(isDelete=='N')
        {
            return;
        }else
        {
            boolean isDeleteSuccess = customerList.deleteCustomer(num-1);//num-1 原理同上相同
            if(isDeleteSuccess)
            {
                System.out.println("-----------------删除成功----------");
            }else
            {
                System.out.println("-----------------删除失败----------");
            }

        }

    }
    private void listAllCustomers(){
        System.out.println("-----------------客户列表------------\n");
        int total = customerList.getTotal();
        if(total==0)
        {
            System.out.println("没有客户记录！");
        }else
        {
            System.out.println("编号\t\t\t姓名\t\t\t性别\t\t\t年龄\t\t\t电话\t\t\t\t邮箱");
            Customer[] allCustomers = customerList.getAllCustomers();
            for(int i=0;i<allCustomers.length;i++)
            {
                System.out.println((i+1)+"\t\t\t\t"
                        +allCustomers[i].getName()+"\t\t\t"
                        +allCustomers[i].getGender()+"\t\t\t"+allCustomers[i].getAge()+"\t"+allCustomers[i].getPhone()+"\t"+allCustomers[i].getEmail());
            }
        }

        System.out.println("-----------------客户列表完成--------\n");

    }

    private void selectCustomer(){
        System.out.println("-----------------客户列表------------\n");
        Customer cust;
        int num;
        while(true)
        {
            System.out.print("请选择查找客户的编号(-1退出)");
            num = CMUtility.readInt();
            if(num==-1)
            {
                return;
            }
            cust = customerList.getCustomer(num-1);//
            if(cust==null)
            {
                System.out.println("无法找到指定客户！");
            }else
            {
                break;
            }
        }
        System.out.println("编号\t\t\t姓名\t\t\t性别\t\t\t年龄\t\t\t电话\t\t\t\t邮箱");
        System.out.println(num+"\t"
                +cust.getName()+"\t"
                +cust.getGender()+"\t"+cust.getAge()+"\t"+cust.getPhone()+"\t"+cust.getEmail());
    }
}
